<style lang="less">
  @import '../../styles/common.less';
</style>

<template>
  <Form ref="masterData" :model="masterData" :rules="ruleValidate" :label-width="110">
    <div class="ivu-row" style="border-bottom: 1px #ccc solid; margin-bottom: 20px;">
      <div style="float: left; width: 45%;border-right: 1px #ccc solid; padding-right: 20px;">
        <FormItem label="姓名：" prop="masterName">
          <Input v-model="masterData.masterName" placeholder="请输入姓名"></Input>
        </FormItem>
        <FormItem label="职称：" prop="masterRank">
          <Input v-model="masterData.masterRank" placeholder="请输入职称"></Input>
        </FormItem>

        <FormItem label="缩略图：" prop="pic">
          <Row type="flex" justify="center" align="middle" class="height-100">
            <Upload
              :format="['jpg', 'png', 'jpeg', 'gif', 'bmp', 'svg']"
              action="/school/fileUpload/"
              :on-format-error="handleFormatError" :on-success="handleSuccess" :on-remove="handleRemove">
              <span>选择图片上传&nbsp;&nbsp;</span>
              <Button type="ghost" icon="ios-cloud-upload-outline">上传文件</Button>
            </Upload>
          </Row>
        </FormItem>

      </div>

    </div>


    <FormItem label="研究方向：" prop="rDirection">
      <UE :defaultMsg=defaultMsg :config=config :id=ue1 ref="ue1"></UE>
    </FormItem>

    <FormItem label="主讲课题：" prop="mTopic">
      <UE :defaultMsg=defaultMsg :config=config :id=ue2 ref="ue2"></UE>
    </FormItem>

    <FormItem label="简介：" prop="mDesc">
      <UE :defaultMsg=defaultMsg :config=config :id=ue3 ref="ue3"></UE>
    </FormItem>

    <FormItem>
      <Button type="primary" @click="handleSubmit('masterData')">提交</Button>
      <Button type="ghost" @click="handleReset('masterData')">清空</Button>
    </FormItem>
  </Form>
</template>

<script>
  import moment from 'moment'
  import UE from '../ue/ue.vue';
    export default {
      components: {UE},
        name: "mastermod",
        data(){
          return{
            masterData:{
              masterName:'',
              masterRank:'',
              rDirection:'',
              mTopic:'',
              mDesc:'',
              masterImg:'',
              id:0
            },
            ruleValidate:{
              masterName:[
                {required: true,message:'请输入姓名',trigger:'blur'}
              ],
              masterRank:[
                {required: true,message:'请输入职称',trigger:'blur'}
              ]
            },
            defaultMsg: '',
            config: {
              initialFrameWidth: null,
              initialFrameHeight: 350
            },
            ue1: "ue1", // 不同编辑器必须不同的id
            ue2: "ue2", // 不同编辑器必须不同的id
            ue3: "ue3", // 不同编辑器必须不同的id
          }
        },
        methods:{
          handleSubmit(name){
            this.$refs[name].validate((valid) => {
              if (valid) {
                let dataArr = {
                  masterName:this.masterData.masterName,
                  masterRank:this.masterData.masterRank,
                  rDirection:this.$refs.ue1.getUEContent(),
                  mTopic:this.$refs.ue2.getUEContent(),
                  mDesc:this.$refs.ue3.getUEContent(),
                  masterImg:this.masterData.masterImg,
                  id:this.masterData.id
                };

                if(this.masterData.id > 0){
                  this.$http.post('/school/updMaster', dataArr).then(res => {
                    this.$Message.success('更新成功!');
                    this.handleReset(name);
                  });
                }else {
                  this.$http.post('/school/addMaster', dataArr).then(res => {
                    this.$Message.success('更新成功!');
                    this.handleReset(name);
                  });
                }
              }
            })
          },
          handleReset (name) {
            this.$refs[name].resetFields();
          },
          handleFormatError (file) {
            this.$Notice.warning({
              title: '文件格式不正确',
              desc: '文件 ' + file.name + ' 格式不正确，请选择图片文件。'
            });
          },
          handleSuccess(response, file, fileList){
            this.masterData.masterImg = response.data[0];
          },
          handleRemove(file, fileList){
            this.masterData.masterImg = '';
          },
        },
      mounted () {
        if(this.$route.query.id == undefined) return;
        this.$http.get('/school/getMasterById?id='+this.$route.query.id).then(res=>{
          var dt = res.data.data;
          this.masterData.id = dt.id;
          this.masterData.masterName = dt.masterName;
          this.masterData.masterRank = dt.masterRank;
          this.masterData.rDirection = dt.rDirection;
          this.masterData.mTopic = dt.mTopic;
          this.masterData.mDesc = dt.mDesc;
          this.masterData.masterImg = dt.masterImg;

          this.$refs.ue1.setUEContent(dt.rDirection);
          this.$refs.ue2.setUEContent(dt.mTopic)
          this.$refs.ue3.setUEContent(dt.mDesc)

        });
      },
      created () {

      }
    }
</script>

<style scoped>

</style>
